
$(function() {
    var str=$.cookie("rmbNoteUserInfo");
    if(str!=undefined && str.length>0){
       var userInfo = JSON.parse(AESDecrypt(str));
       $("#loginName").textbox("setValue",userInfo.loginName);
       $("#password").passwordbox("setValue",userInfo.password);
       $("#rmbUser").switchbutton("check");
    }else{
        $("#rmbUser").switchbutton("uncheck");
    }
    //alert($("#rmbUser").switchbutton("options").checked);
    //回车事件
	$('#password').passwordbox('textbox').keydown(function (e) {
        if(e.keyCode == 13) {
           login();
        }
    });	
});

function login(){
    var loginName = $("#loginName").textbox("getValue"); //$("#login_form").find("input[name='loginName']").val();
    var password = $("#password").passwordbox("getValue"); //$("#login_form").find("input[name='password']").val();
    if(loginName.length == 0){
        $("#errMsgBar").show();
        $("#errMsgBar").html("请输入用户名");
        return;
    }
    if(loginName.length > 30){
        $("#errMsgBar").show();
        $("#errMsgBar").html("用户名长度超过限制");
        return;
    }  

    if(password.length == 0){
        $("#errMsgBar").show();
        $("#errMsgBar").html("请输入密码");
        return;
    }
    if(password.length > 30){
        $("#errMsgBar").show();
        $("#errMsgBar").html("密码长度超过限制");
        return;
    }  
  
    var param = {
        "loginName":loginName,
        "password":password
        };

    remenberMe();
    ///alert(JSON.stringify(param));

    $("#loginBtn").linkbutton('disable');
    $.ajax({
       // headers: {
        //    app: 'PLATFORM',
        //    platform: 'PC',
        //    imei: ''
       // },
        type:"post",
        url:"/note/login_do",
        //contentType:"application/json;charset=utf-8",
        data:{param:AESEncrypt(param)},
        dataType:'json',
        success:function(ret){
            if(ret.code == 0){ //成功
               $.cookie('token',ret.data,{expires:30, path:'/'})
               window.location.href = "/note/main";
            }else{
              //alert(ret.msg);
                $("#errMsgBar").show();
                $("#errMsgBar").html(ret.msg);
            }
        },
        error:function(ret) {
            alert(JSON.stringify(ret));
        },
        complete:function () {
            $("#loginBtn").linkbutton('enable');
        }
    });

}

function logout(){
    $.cookie("token", "", {expires:-1, path:'/'});
}
function remenberMe(){
 if ($("#rmbUser").switchbutton("options").checked) {  
        var userInfo={
                loginName:$("#loginName").textbox("getValue"),
                password:$("#password").passwordbox("getValue")
            }
        $.cookie("rmbNoteUserInfo", AESEncrypt(userInfo), {expires:7, path:'/'}); // 存储一个带7天期限的 cookie  
    } else {  
        $.cookie("rmbNoteUserInfo", "", {expires:-1, path:'/'});
    } 
}
